
//
//  MineController.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/9/12.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "MineController.h"
#import "MineHeaderView.h"
#import "AboutUsController.h"
#import "MineNewsController.h"
#import "MineMessageController.h"
#import "MineSystemSetController.h"

@interface MineController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,copy) UITableView *ownTableView;
@property (nonatomic,strong) MineHeaderView *headerView;
@property (nonatomic,copy) NSArray *imgArr;
@property (nonatomic,copy) NSArray *titleArr;
@end

@implementation MineController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
    [[CoachTabBarController shareLZHTabbarController].tabBar setHidden:NO];
    [self refreshData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSubViews];
}


#pragma mark ==========创建视图==========
- (void)createSubViews{
    self.imgArr = @[@"7-1-3",@"MineSetImg",@"MineAboutUs"];
    self.titleArr = @[@"我的消息",@"系统设置",@"关于我们"];
    [self.view addSubview:self.ownTableView];
}

#pragma mark ==========刷新数据==========
- (void)refreshData{
    [self.ownTableView reloadData];
    self.headerView.model = [UserMessage sharedInstance].model;
}

- (UITableView *)ownTableView{
    
    if (_ownTableView == nil) {
        _ownTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - [CoachTabBarController shareLZHTabbarController].tabBar.frame.size.height) style:UITableViewStylePlain];
        _ownTableView.tableHeaderView = self.headerView;
        _ownTableView.tableFooterView = [[UIView alloc]init];
        _ownTableView.backgroundColor = [UIColor clearColor];
        _ownTableView.delegate = self;
        _ownTableView.dataSource = self;
        _ownTableView.showsVerticalScrollIndicator = NO;
        _ownTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
        [_ownTableView setSeparatorInset:UIEdgeInsetsMake(0, 30*SCALEUNIT, 0, 30*SCALEUNIT)];
        [_ownTableView setSeparatorColor:RGBA(230, 230, 230, 1)];
        [_ownTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MineController"];
    }
    return _ownTableView;
}
- (MineHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[MineHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 390*SCALEUNIT)];
        [_headerView.imgBtn addTarget:self action:@selector(jumpToMineMessagePage:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerView;
}

#pragma mark ==========UITableView代理方法==========
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 95*SCALEUNIT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineController" forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:self.imgArr[indexPath.row]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titleArr[indexPath.row];
    cell.textLabel.textColor = RGBA(70, 70, 70, 1);
    cell.textLabel.font = [UIFont systemFontOfSize:LoginFont-2];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIViewController *VC;
    if (indexPath.row == 0) {
        VC = [[MineNewsController alloc]init];
    } else if (indexPath.row == 1){
        VC = [[MineSystemSetController alloc] init];
    } else {
        VC = [[AboutUsController alloc] init];
    }
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark ==========滚动tableview完毕之后==========
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //图片高度
    CGFloat imageHeight = self.headerView.frame.size.height;
    //图片宽度
    CGFloat imageWidth = self.headerView.frame.size.width;
    //图片上下偏移量
    CGFloat imageOffsetY = scrollView.contentOffset.y;
    
    //NSLog(@"图片上下偏移量 imageOffsetY:%f ->",imageOffsetY);
    //上移
    if (imageOffsetY < 0) {
        CGFloat totalOffset = imageHeight + ABS(imageOffsetY);
        CGFloat f = totalOffset / imageHeight;
        self.headerView.backImgView.frame = CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset);
    }
}

#pragma mark ==========跳转至个人信息设置界面==========
- (void)jumpToMineMessagePage:(UIButton *)btn{
    MineMessageController *messageVC = [[MineMessageController alloc] init];
    messageVC.block = ^(UIImage *headerImg){
        [self.headerView.imgBtn setImage:headerImg forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:messageVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
